package com.example.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.model.common.dtos.ResponseResult;
import com.example.model.common.enums.AppHttpCodeEnum;
import com.example.model.user.dtos.AdUserDto;
import com.example.model.user.pojos.AdUser;
import com.example.user.lister.RabbitMQLister;
import com.example.user.mapper.AdUserMapper;
import com.example.user.service.UserLoginService;
import com.example.utils.common.AppJwtUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.util.HashMap;
import java.util.Map;


@Service
@Transactional
public class UserLoginServiceImpl extends ServiceImpl<AdUserMapper, AdUser> implements UserLoginService {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Override
    public ResponseResult login(AdUserDto dto) {
        //1.参数校验
        if (StringUtils.isEmpty(dto.getUsername()) || StringUtils.isEmpty(dto.getPassword())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE, "用户名或密码不能为空");
        }
        LambdaQueryWrapper<AdUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(AdUser::getUsername,dto.getUsername());
        AdUser adUser = this.baseMapper.selectOne(lambdaQueryWrapper);
        if (adUser!=null){
            String pswd = DigestUtils.md5DigestAsHex((dto.getPassword() + adUser.getSalt()).getBytes());
            if (pswd.equals(adUser.getPassword())){
                Map<String, Object> map = new HashMap<>();
                map.put("token", AppJwtUtil.getToken(adUser.getId().longValue()));
                map.put("user", adUser);
                return ResponseResult.okResult(map);
            }else {
                return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
            }
        }else {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "用户不存在");
        }
    }

    @Override
    public ResponseResult loginTwo(AdUserDto adUserDto) {
        LambdaQueryWrapper<AdUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(AdUser::getPhone, adUserDto.getPhone());
        AdUser adUser = this.baseMapper.selectOne(lambdaQueryWrapper);
        if (adUser != null) {
            String redisCode = stringRedisTemplate.opsForValue().get(adUserDto.getPhone());
            if (redisCode != null && redisCode!= "") {
                if (redisCode.equals(adUserDto.getCode())) {
                    Map<String, Object> map = new HashMap<>();
                    map.put("token", AppJwtUtil.getToken(adUser.getId().longValue()));
                    map.put("user", adUser);
                    return ResponseResult.okResult(map);
                } else {
                    return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "验证码输入错误");
                }
            } else {
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "验证码超期");
            }
        } else {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "用户不存在");
        }
    }

    @Override
    public void sendCode(String phone) {
        RabbitMQLister rabbitMQLister = new RabbitMQLister();
        rabbitTemplate.convertAndSend("direct.exchange","pian",phone);
        rabbitMQLister.sendCode(phone);
    }


}
